//个人填报记录

const app = getApp();
var util = require('../../utils/util.js');
var timestamp = Date.parse(new Date());


Page({
  data: {
    diaglogt :true,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    // imgalist: ['https://7a6d-zmyy-78bd28-1258642890.tcb.qcloud.la/txl.jpg?sign=bccefd9f27cecdde8f3d7e5fff5eae6e&t=1551966525']
  },

  on_delete: function (e) {///删除当前记录并返回上次页面
    console.log('马上查询数据库，opneid为:', e.currentTarget.dataset)
    const db = wx.cloud.database()      
    db.collection('base').doc(e.currentTarget.dataset.index._id).remove({
      success: function (res) {
        this.setData ({
          list_n: e.currentTarget.dataset.index
        })
      }
    })
    this.findOpenid();
  },
  findOpenid: function () {
    const db = wx.cloud.database();
    wx.cloud.callFunction({
      name: 'login',
      complete: res => {
        app.globalData.openid = res.result.openid
        // console.log('onGetOpenid result: ', app.globalData.openid)
        db.collection('base').where({
          _openid: app.globalData.openid
        })
          .get()
          .then(res => {
            // console.log('马上查询数据库，opneid为:', app.globalData.openid)
            if (res.data.length) {
              console.log('res.data', res.data)
              this.setData({
                list_n: res.data,
               
              })

            
            }
            else {
               console.log('没查询到数据', res.data)
            
            }
          })
      }
    })


  },
  bodadianhua: function (res) {
    this.setData({
      diaglogt: false,
    })
   
  },
  //确定按钮点击事件
  diag_en: function () {
    this.on_delete();
    this.findOpenid();
    wx.showToast({
      title: '上报成功',
      icon: 'success',
    })

    this.setData({
      diaglogt: true,
    })

  },
  //取消按钮点击事件
  diag_ca: function () {
    this.setData({
      diaglogt: true,
    })
  },
  onLoad: function () {
    this.findOpenid();
    if (!wx.cloud) {
      wx.redirectTo({
        url: '../chooseLib/chooseLib',
      })
      return
    }
  },

	/** 
	 * 预览图片
	 */
  previewImage: function (e) {
    var current = e.target.dataset.src;
    wx.previewImage({
      current: current, // 当前显示图片的http链接
      urls: this.data.imgalist // 需要预览的图片http链接列表
    })
  }


})
